package com.shd.edanpei.ui.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.shd.edanpei.R;
import com.shd.edanpei.base.BaseActivity;
import com.shd.edanpei.base.BaseIPresenter;
import com.shd.edanpei.base.network.api.ApiCreator;
import com.shd.edanpei.base.network.exception.ResponeThrowable;
import com.shd.edanpei.base.network.model.NoDataResponse;
import com.shd.edanpei.base.network.rx.BaseObserver;
import com.shd.edanpei.base.network.rx.BaseResponse;
import com.shd.edanpei.base.network.rx.RxTransformer;
import com.shd.edanpei.bean.PagingModel;
import com.shd.edanpei.bean.SuggestBean;
import com.shd.edanpei.ui.adapter.RecordAdapter;
import com.shd.edanpei.util.LayoutUtils;

import org.w3c.dom.Text;

import butterknife.BindView;
import butterknife.OnClick;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;

public class OpinionRecordActivity extends BaseActivity {

    @BindView(R.id.headline)
    TextView headline;
    @BindView(R.id.recycler_view)
    RecyclerView recyclerView;

    RecordAdapter mAdapter;


    private int currentPage;
    private int totalPage;

    @Override
    protected int setLayout() {
        return R.layout.activity_opinion_record;
    }

    @Override
    protected BaseIPresenter bindPresenter() {
        return null;
    }

    @Override
    protected void initView(@Nullable Bundle savedInstanceState) {
        headline.setText("反馈记录");

        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        mAdapter = new RecordAdapter(this);
        recyclerView.setAdapter(mAdapter);
        getData();
    }

    @Override
    protected void initListener() {
        mAdapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
            @Override
            public void onLoadMoreRequested() {
                loadMore();
            }
        },recyclerView);
    }

    @OnClick(R.id.iv_headline_back)
    void onBack() {
        finish();
    }

    private void getData() {
        currentPage = 1;
        ApiCreator.getApi().suggestList()
                .compose(RxTransformer.<PagingModel<SuggestBean>>errorHandle())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<PagingModel<SuggestBean>>() {
                    @Override
                    protected void onSuccess(PagingModel<SuggestBean> data) {
                        if (data != null && data.getList().size() > 0) {
                            totalPage = data.getTotalPage();
                            mAdapter.setNewData(data.getList());
                            if (currentPage >= totalPage) {
                                mAdapter.loadMoreEnd();
                            } else {
                                mAdapter.loadMoreComplete();
                            }
                            currentPage++;
                        } else {
                            mAdapter.setNewData(null);
                            mAdapter
                                    .setEmptyView(LayoutUtils.getNullLayout(getContext(), R.drawable.pic_null_bg, "暂无反馈记录"));
                        }
                    }
                });
    }

    private void loadMore(){
        ApiCreator.getApi().suggestList()
                .compose(RxTransformer.<PagingModel<SuggestBean>>errorHandle())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<PagingModel<SuggestBean>>() {
                    @Override
                    protected void onSuccess(PagingModel<SuggestBean> data) {
                        if (data != null && data.getList().size() > 0) {
                            mAdapter.addData(data.getList());
                            if (currentPage >= totalPage) {
                                mAdapter.loadMoreEnd();
                            } else {
                                mAdapter.loadMoreComplete();
                            }
                            currentPage++;
                        } else {
                            mAdapter.loadMoreEnd();
                        }
                    }

                    @Override
                    public void onFailure(ResponeThrowable throwable) {
                        mAdapter.loadMoreEnd();
                    }
                });
    }
}
